package com.nd.service.wx;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.CommentMapper;
import com.nd.dao.UserMapper;
import com.nd.pojo.bean.Comment;
import com.nd.pojo.bean.CommentExample;
import com.nd.pojo.bean.User;
import com.nd.pojo.bean.UserExample;
import com.nd.pojo.bo.wx.CommentPostBO;
import com.nd.pojo.vo.wx.CommentCountVO;
import com.nd.pojo.vo.wx.CommentListVO;
import com.nd.pojo.vo.wx.CommentPostVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: Aaron
 * @create: 2021-06-08 15:23
 **/
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public CommentListVO commentList(Integer valueId, Byte type, Integer size, Integer page, Integer showType) {
        PageHelper.startPage(page,size);
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        if (showType == 1) criteria.andHasPictureEqualTo(true);//带图
        criteria.andTypeEqualTo(type).andValueIdEqualTo(valueId);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        long total = commentPageInfo.getTotal();
        List<CommentListVO.ListBean> data = new ArrayList<>();
        for (Comment comment : comments) {
            UserExample userExample = new UserExample();
            UserExample.Criteria criteriaUser = userExample.createCriteria();
            criteriaUser.andIdEqualTo(comment.getUserId());
            List<User> users = userMapper.selectByExample(userExample);
            CommentListVO.UserInfos infos = CommentListVO.UserInfos.builder().nickName(users.get(0).getNickname()).avatarUrl(users.get(0).getAvatar()).build();
            CommentListVO.ListBean listBean = CommentListVO.ListBean.builder().userInfo(infos).addTime(comment.getAddTime()).picList(comment.getPicUrls()).content(comment.getContent()).build();
            data.add(listBean);
        }
        CommentListVO commentListVO = CommentListVO.builder()
                .data(data)
                .count(comments.size())
                .currentPage(total).build();
        return commentListVO;
    }

    @Override
    public CommentCountVO commentCount(Integer valueId, Byte type) {
        CommentCountVO commentCountVO = new CommentCountVO();

        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId).andTypeEqualTo(type);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        criteria.andHasPictureEqualTo(true);
        List<Comment> commentsHaspic = commentMapper.selectByExample(commentExample);
        commentCountVO.setAllCount(comments.size());
        commentCountVO.setHasPicCount(commentsHaspic.size());
        return commentCountVO;
    }

    @Override
    public CommentPostVO commentPost(CommentPostBO commentPostBO) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User)subject.getPrincipal();
        Integer userId = user.getId();

        DateTime time = DateTime.now();

        Comment comment = Comment.builder()
                .valueId(commentPostBO.getValueId())
                .type(commentPostBO.getType())
                .content(commentPostBO.getContent())
                .userId(userId)
                .hasPicture(commentPostBO.isHasPicture())
                .picUrls(commentPostBO.getPicUrls())
                .star(commentPostBO.getStar())
                .addTime(time)
                .updateTime(time)
                .deleted(false).build();
        commentMapper.insertSelective(comment);
        Integer id =  commentMapper.getId();
        CommentPostVO commentPostVO = CommentPostVO.builder()
                .id(id)
                .valueId(commentPostBO.getValueId())
                .type(commentPostBO.getType())
                .content(commentPostBO.getContent())
                .userId(userId)
                .hasPicture(commentPostBO.isHasPicture())
                .picUrls(commentPostBO.getPicUrls())
                .star(commentPostBO.getStar())
                .addTime(time)
                .updateTime(time)
                .build();
        return commentPostVO;
    }
}
